package com.atguigu.day07;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

import java.util.ArrayList;
import java.util.List;

public class Flink06_Connect {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从元素中获取数据
        DataStreamSource<Integer> numStream = env.fromElements(1,2,3,4,5,6);


        DataStreamSource<String> strStream = env.fromElements("a", "b", "c", "d", "e");

        //TODO 3.使用Connect将两条流合并到一起
        ConnectedStreams<Integer, String> connectedStreams = numStream.connect(strStream);

        SingleOutputStreamOperator<String> map = connectedStreams.map(new CoMapFunction<Integer, String, String>() {
            private List<Integer> list = new ArrayList<>();

            @Override
            public String map1(Integer value) throws Exception {
                list.add(value);
                return value * value + "";
            }

            @Override
            public String map2(String value) throws Exception {
                for (Integer integer : list) {

                }
                return value + "aaaaa";
            }
        });

        map.print();


        env.execute();


    }
}
